﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration; 

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTAdmixturePowder : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method

        private void BindGridView()
        {
            RPTAdmixturePowderService RPTAdmixturePowderServiceObj = ServiceFactory.getRPTAdmixturePowderService();
            OperationResult<List<tb_RPT_AdmixturePowder>> result = RPTAdmixturePowderServiceObj.GetAllAdmixturePowder();

            if (result.operationSuccess)
            {
                gvAdmixturePowderList.DataSource = result.obj;
                gvAdmixturePowderList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTAdmixturePowderService RPTAdmixturePowderServiceObj = ServiceFactory.getRPTAdmixturePowderService();
            OperationResult<tb_RPT_AdmixturePowder> resultEdit = RPTAdmixturePowderServiceObj.GetAdmixturePowderById(id);

            if (resultEdit.operationSuccess == true)
            {
                txtEditSequence.Text = resultEdit.obj.Sequence;
                txtEditFeedDate.Text = resultEdit.obj.FeedDate.ToString ();
                txtEditFeedWeight.Text = resultEdit.obj.FeedWeight.ToString(); 
                txtEditRemark.Text = resultEdit.obj.Remark; 
                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTAdmixturePowderService RPTAdmixturePowderServiceObj = ServiceFactory.getRPTAdmixturePowderService();
            tb_RPT_AdmixturePowder instance = new tb_RPT_AdmixturePowder();

            instance.Sequence = txtSequence.Text;
            instance.FeedDate = DateTime.Parse( txtFeedDate.Text);
            instance.FeedWeight = txtFeedWeight.Text;
             instance.Remark = txtRemark.Text;

             OperationResult<tb_RPT_AdmixturePowder> result = RPTAdmixturePowderServiceObj.CreateAdmixturePowder(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            RPTAdmixturePowderService RPTAdmixturePowderServiceObj = ServiceFactory.getRPTAdmixturePowderService();
            tb_RPT_AdmixturePowder entity = new tb_RPT_AdmixturePowder();

            entity.Id = editId;
            entity.Sequence = txtEditSequence.Text;
            entity.FeedDate =DateTime.Parse ( txtEditFeedDate.Text);
            entity.FeedWeight = txtEditFeedWeight.Text;
             entity.Remark = txtEditRemark.Text;

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_AdmixturePowder> resultEdit = RPTAdmixturePowderServiceObj.ModifyAdmixturePowder(entity);

            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTAdmixturePowderService RPTAdmixturePowderServiceObj = ServiceFactory.getRPTAdmixturePowderService();
            tb_RPT_AdmixturePowder deleteEntity = new tb_RPT_AdmixturePowder();
            deleteEntity.Id = deleteId;
            RPTAdmixturePowderServiceObj.DeleteAdmixturePowder(deleteEntity);

            BindGridView();
        }

        protected void gvAdmixturePowderList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvAdmixturePowderList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAdmixturePowderList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
